package com.gaogzhen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.gaogzhen.domain.SysMenu;
import com.gaogzhen.domain.SysPrivilege;
import com.gaogzhen.service.SysMenuService;
import com.gaogzhen.service.SysPrivilegeService;
import com.gaogzhen.utils.RolePrivilegesParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaogzhen.domain.SysRolePrivilege;
import com.gaogzhen.mapper.SysRolePrivilegeMapper;
import com.gaogzhen.service.SysRolePrivilegeService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

@Service
public class SysRolePrivilegeServiceImpl extends ServiceImpl<SysRolePrivilegeMapper, SysRolePrivilege> implements SysRolePrivilegeService{

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysPrivilegeService sysPrivilegeService;

    @Autowired
    private SysRolePrivilegeService sysRolePrivilegeService;

    @Override
    public List<SysMenu> findMenusAndPrivileges(Long roleId) {
        // 查询所有菜单
        List<SysMenu> menus = sysMenuService.list();
        if (CollectionUtils.isEmpty(menus)) {
            return Collections.emptyList();
        }
        // 一级菜单
        List<SysMenu> rootMenus = menus.stream()
                .filter(m -> m.getParentId() == null)
                .collect(Collectors.toList());
        if (CollectionUtils.isEmpty(rootMenus)) {
            return Collections.emptyList();
        }
        // 递归封装子菜单
        List<SysMenu> subMenus = new ArrayList<>();
        for (SysMenu rootMenu : rootMenus) {
            // 设置子菜单
            subMenus.addAll(getChildMenus(rootMenu.getId(), roleId, menus));
//            rootMenu.setSubMenus(subMenus);
        }
        return subMenus;
    }

    @Override
    @Transactional
    public boolean grantPrivileges(RolePrivilegesParam rolePrivilegesParam) {
        // 1删除角色原有权限
        Long roleId = rolePrivilegesParam.getRoleId();
        sysRolePrivilegeService.remove(new LambdaQueryWrapper<SysRolePrivilege>()
                .eq(SysRolePrivilege::getRoleId, roleId));

        // 2授予新的权限
        List<Long> privilegeIds = rolePrivilegesParam.getPrivilegeIds();
        if (!CollectionUtils.isEmpty(privilegeIds)) {
            List<SysRolePrivilege> sysRolePrivileges = privilegeIds.stream().map(pid -> {
                SysRolePrivilege sysRolePrivilege = new SysRolePrivilege();
                sysRolePrivilege.setPrivilegeId(pid);
                sysRolePrivilege.setRoleId(roleId);
                return sysRolePrivilege;
            }).collect(Collectors.toList());
            boolean b = sysRolePrivilegeService.saveBatch(sysRolePrivileges);
            return b;

        }
        return true;
    }

    /**
     * 查询菜单的子菜单包含权限
     * @param parentId  父菜单id
     * @param roleId    当前角色id
     * @param menus     所有菜单集合
     * @return
     */
    private List<SysMenu> getChildMenus(Long parentId, Long roleId, List<SysMenu> menus) {
        // 查询子菜单
        List<SysMenu> child = menus.stream()
                .filter(s -> ObjectUtils.nullSafeEquals(s.getParentId(), parentId))
                .collect(Collectors.toList());
        if (CollectionUtils.isEmpty(child)) {
            return Collections.emptyList();
        }
        for (SysMenu menu : child) {
            // 递归设置下一级菜单
            List<SysMenu> childMenus = getChildMenus(menu.getId(), roleId, menus);
            menu.setSubMenus(childMenus);
            // 设置权限
            List<SysPrivilege> privileges = sysPrivilegeService.getPrivileges(menu.getId(), roleId);
            menu.setPrivileges(privileges);
        }
        // 查询下级菜单
        // 查询权限
        return child;
    }
}
